package com.hawkfly.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.config.Config;
import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.serveraction.IServerAction;

/**
 * 使用AJAX的方式检测用户账号是否存在之Action
 * @author hawkfly
 */
public class CheckUserAction implements IServerAction
{
    private static Log log=LogFactory.getLog(CheckUserAction.class);
    public String executeSeverAction(ReportRequest rrequest,IComponentConfigBean ccbean,List<Map<String,String>> lstData,
            Map<String,String> mCustomizedData)
    {
        // TODO Auto-generated method stub
        printLstParams(lstData);
        String rtnStatus = "0";
        //以usercode为条件在表CBSS_MEMBER中进行查询，看能否查询到数据。如果查询到数据返回1，否则返回0
        if(lstData == null)return rtnStatus;
        String usercode = lstData.get(0).get("MEMBER_CODE");
        usercode = usercode == null ? "":usercode;
        Connection conn = rrequest.getConnection();
        String sql = "select question1 from CBSS_MEMBER where MEMBER_CODE = ?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try
        {
            ps = conn.prepareStatement(sql);
            ps.setString(1,usercode);
            rs = ps.executeQuery();
            if(rs.next()){
                StringBuffer sb = new StringBuffer();
                sb.append("{question1:'");
                sb.append(rs.getString(1));
                sb.append("',MEMBER_CODE:'");
                sb.append(usercode);
                sb.append("'}");
                rtnStatus = sb.toString();
            }
        }catch(SQLException e)
        {
            // TODO Auto-generated catch block
           log.error("在执行用户名查询业务是出现数据查询异常",e);
        }finally{
            try
            {
                rs.close();
                ps.close();
                conn.close();
            }catch(SQLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
        }
        return rtnStatus;
    }

    /* 
     * 专门用来处理找回密码的业务逻辑调用
     */
    public String executeServerAction(HttpServletRequest request,HttpServletResponse response,List<Map<String,String>> lstData)
    {
        // TODO Auto-generated method stub
        //客户端数据整理
        Map<String, String> ctmParamsMap = new HashMap<String, String>();
        for(Map<String, String> item: lstData){
            Iterator<String> it = item.keySet().iterator();
            while(it.hasNext()){
                String key = it.next();
                ctmParamsMap.put(key,item.get(key));
            }
        }
        //变量初始化
        String msg = "问题回答错误！";
        Connection conn = Config.getInstance().getDataSource("ds_oracle").getConnection();
        String checkUserSql = "SELECT * FROM cbss_member where member_code=? and question1=? and answer1=?";
        String updatePwdSql = "UPDATE cbss_member SET mem_password=? where member_code=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        //根据用户名，问题及答案查询是否能够查询到有效数据
        try
        {
           ps = conn.prepareStatement(checkUserSql);
           ps.setString(1,ctmParamsMap.get("MEMBER_CODE"));
           ps.setString(2,ctmParamsMap.get("question1"));
           ps.setString(3,ctmParamsMap.get("answer1"));
           rs = ps.executeQuery();
         //查询到数据则根据用户名更新密码并返回更新成功信息
           if(rs.next()){
              PreparedStatement ups = conn.prepareStatement(updatePwdSql);
              ups.setString(1,ctmParamsMap.get("MEM_PASSWORD"));
              ups.setString(2,ctmParamsMap.get("MEMBER_CODE"));
              ups.executeUpdate();
              msg = "密码重置成功！";
           }
        }catch(SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try
            {
                rs.close();
                ps.close();
                conn.close();
            }catch(SQLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //查询不到数据则返回问题回答错误
        return msg;
    }
    
    private void printLstParams(List<Map<String,String>> lstData)
    {
        if(lstData==null||lstData.size()==0)
        {
            System.out.println("无参数");
        }else
        {
            System.out.print("客户端传入参数列表：：：");
            for(Map<String,String> mParams:lstData)
            {
                StringBuffer paramBuf=new StringBuffer();
                for(Entry<String,String> entryTmp:mParams.entrySet())
                {
                    paramBuf.append("[paramname:"+entryTmp.getKey()+";paramvalue:"+entryTmp.getValue()+"]");
                }
                System.out.print("{"+paramBuf.toString()+"};");
            }
            System.out.println(" ");
        }
    }

}

